var point = {
    id: "",
    isdown: false,
    top: 0,
    left: 0,
    width: 0,
    height: 0
};
(function (tc) {
    var default_option = {
        width: 800,
        height: 500,
        url: "#",
        title: "此处填写标题",
        content: "此处填写内容",
        onconfirm: $.noop,
        oncancel: $.noop,
        isconfirm: true
    };

    tc.tc = {
        option: default_option,
        uuid: function () {
            return getuuid();
        },
        loading: function (div) {
            var load_html = $(".ui_win_loading").html();
            if (load_html == null || load_html == "" || load_html != undefined) {
                load_html = '<div class="ui_win_loading" style="border:0;"><div class="loading_img" style="width: 66px; height: 6px;margin-top:10px;margin-left: calc(50% - 33px);border:0;"><img src="/Content/images/loading.gif" alt=""></div></div>';
                $(div).append(load_html);
            } else {
                tc.alert("检测到已存在的加载框");
            }
        },
        loadexit: function () {
            var load_win = $(".ui_win_loading");
            var load_html = $(".ui_win_loading").html();
            if (load_html != null && load_html != "" && load_html != undefined) {
                $(load_win).remove();
            } else {
                //tc.alert("未检测到进行中的加载框");
            }
        },
        alert: function (content) {
            var alert_html = $(".ui_win_alert").html();
            if (alert_html != null || alert_html != "" || alert_html != undefined) {
                content = content == undefined ? "" : content;
                alert_html = '<div class="ui_win_alert" ><div class="alert_info"><div class="info_title">网页消息</div><div class="info_content">' + content + '</div><div class="info_btns"><a href="#" class="tc_img_btn" onclick="javascript:tc.alertexit();"><img src="/Content/icons/success.png" />&nbsp;确定</a></div></div></div>';
                $("body").append(alert_html);
            } else {
                tc.alert("检测到已存在的消息弹窗");
            }
        },
        alertexit: function () {
            var alert_win = $(".ui_win_alert");
            var alert_html = $(".ui_win_alert").html();
            if (alert_html != null && alert_html != "" && alert_html != undefined) {
                $(alert_win).remove();
            } else {
                tc.alert("未检测到进行中的消息弹窗");
            }
        },
        pormpt: function (option) {
            this.option = $.extend({}, this.option, option);
            var pormpt_uuid = getuuid();
            var pormpt_html = '<div id="div_' + pormpt_uuid + '" class="ui_win_pormpt"><div class="alert_info"><div class="info_title">' + this.option.title + '</div><div class="info_content" style="text-align:center;" ><br /><input type="text" id="txt_' + pormpt_uuid + '" value="' + this.option.content + '" /></div><div class="info_btns">';
            pormpt_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.onsave(\'' + pormpt_uuid + '\');" ><img src="/Content/icons/success.png" />&nbsp;确定</a>';
            pormpt_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.oncancel(\'' + pormpt_uuid + '\');" ><img src="/Content/icons/failed.png" />&nbsp;取消</a>';
            pormpt_html += '</div></div></div>';
            $("body").append(pormpt_html);
        },
        progressBar: function (value, div) {
            var progress = $("#tc_ui_progressBar");
            if (progress == null || progress == undefined || progress.length == 0) {
                var pro_html = '<div id="tc_ui_progressBar" style="width:calc(50% - 2px);height:6px; border:1px #d2d2d2 solid; background-color:white;border-radius:3px;margin:10px auto 10px;">';
                pro_html += '<div class="progressBar_value" style="width: 0; height: 6px;transition: width 0.5s; background: linear-gradient(to right, #e09292, #1482b4); border-radius: 2px; "></div></div>';
                $(div).append(pro_html);
            } else {
                var prent_width = parseInt($(progress).css("width"));
                var this_width = prent_width * (value / 100);
                if (value > 100) {
                    $(progress).find(".progressBar_value").css({ "width": prent_width + "px" });
                    $(progress).remove();
                } else {
                    $(progress).find(".progressBar_value").css({ "width": this_width + "px" });
                }
            }
        },
        confirm: function (option) {
            this.option = $.extend({}, this.option, option);
            var confirm_uuid = getuuid();
            var confirm_html = '<div id="div_' + confirm_uuid + '" class="ui_win_alert"><div class="alert_info"><div class="info_title">' + this.option.title + '</div><div class="info_content">' + this.option.content + '</div><div class="info_btns">';
            if (this.option.isconfirm) {
                confirm_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.onconfirm(\'' + confirm_uuid + '\');" ><img src="/Content/icons/success.png" />&nbsp;确定</a>';
            }
            confirm_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.oncancel(\'' + confirm_uuid + '\');" ><img src="/Content/icons/failed.png" />&nbsp;取消</a>';
            confirm_html += '</div></div></div>';
            $("body").append(confirm_html);
        },
        iframe: function (option) {
            var load_html = $(".tc_iframe_win").html();
            if (load_html == undefined) {
                this.option = $.extend({}, this.option, option);
                var iframe_uuid = getuuid();
                var iframe_html = '<div id="div_' + iframe_uuid + '" class="tc_iframe_win" style=" width: ' + this.option.width + 'px; height: ' + this.option.height + 'px; position: fixed; top: calc(50% - ' + (this.option.height / 2) + 'px); left: calc(50% - ' + (this.option.width / 2) + 'px);" data-select="0">' +
                    '<div onmousedown="tc.readmove(\'' + iframe_uuid + '\')" onmouseup="tc.discardmove(\'' + iframe_uuid + '\')" class="info_title">' +
                    '<span class="info_txt">' + this.option.title + '</span>' +
                    '<a href="#" style="display: block; width: 21px; height: 21px;float: right;margin-right:5px;" onclick="javascript:tc.oncancel(\'' + iframe_uuid + '\');">' +
                    '<img src="/Content/icons/close.png" alt="" style="width: 21px; height: 21px;margin-top:-2px;">' +
                    '</a>' +
                    '</div>' +
                    '<div class="info_content">' +
                    '<iframe id="' + iframe_uuid + '" src="' + this.option.url + '" frameborder="0" style="width: 100%;height: 100%;">' +
                    '</iframe>' +
                    '</div>' +
                    '<div class="info_btns">';
                if (this.option.isconfirm) {
                    iframe_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.onconfirm(\'' + iframe_uuid + '\')" ><img src="/Content/icons/success.png" />&nbsp;确定</a>';
                }
                iframe_html += '<a href="#" class="tc_img_btn" onclick="javascript:tc.oncancel(\'' + iframe_uuid + '\')" ><img src="/Content/icons/failed.png" />&nbsp;取消</a>' +
                    '</div>' +
                    '</div>';
                $("body").append(iframe_html);
            } else {
                tc.alert("检测到已存在的窗口，请关闭后在重新尝试。");
            }
        },
        readmove: function (uuid) {
            var parent_div = $("#" + uuid);
            point.id = uuid;
            point.top = parseInt($(parent_div).css("top"));
            point.left = parseInt($(parent_div).css("left"));
            point.width = parseInt($(parent_div).css("width"));
            point.height = parseInt($(parent_div).css("height"));
            option.isdown = true;
        },
        discardmove: function () {
            point.id = "";
            point.top = 0;
            point.left = 0;
            point.width = 0;
            point.height = 0;
            option.isdown = false;
        },
        startmove: function () {
            //尚未实现
        },
        toptips: function (type, title) {
            //0红1绿2黑3橙
            var toptips_uuid = getuuid();
            var colorArr = new Array("e64340", "04be02", "2f2f2f", "e1662f");
            var toptips_html = '<div id="' + toptips_uuid + '" class="tc_toptips" style="display:none;position: fixed; bottom: 0; left: 0; width: 100% !important;padding-top:6px; height: 26px; background: #' + colorArr[type] + '; text-align: center; font-size: 14px; color:#fff ; ">' + title + '</div>';
            $("body").append(toptips_html);
            $("#" + toptips_uuid).fadeIn(300);
            setTimeout(function () { $("#" + toptips_uuid).fadeOut(300, function () { $("#" + toptips_uuid).remove(); }); }, 1500);
        },
        oncancel: function (uuid) {
            this.option.oncancel(uuid);
            $("#div_" + uuid).remove();
        },
        onconfirm: function (uuid) {
            this.option.onconfirm(uuid);
        },
        onsave: function (uuid) {
            var value = $("#txt_" + uuid).val();
            this.option.onconfirm(uuid, value);
        }
    };
})(window);

function getuuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
};